package com.example.huwang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.huwang.R;
import com.example.huwang.model.LoginBean;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.huwang.app.MainApplication.dbController;

public class RegisterActivity extends  BaseActivity {
    String typeStr;

    @BindView(R.id.type)
    public TextView type;

    @BindView(R.id.user_phone)
    public EditText userPhone;
    @BindView(R.id.code_word)
    public EditText codeWord;
    @BindView(R.id.user_psw)
    public EditText userPsw;
    @BindView(R.id.user_psw_double)
    public EditText userPswDouble;

    @BindView(R.id.get_code)
    public TextView getCode;
    @BindView(R.id.login_btn)
    public Button loginBtn;

    @BindView(R.id.return_btn)
    public ImageView returnBtn;
    @BindView(R.id.return_text)
    public TextView returnText;
    @BindView(R.id.title)
    public TextView title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //绑定初始化ButterKnife
        ButterKnife.bind(this);
        typeStr = getIntent().getStringExtra("type");
        initView();
    }

    private void initView() {
        title.setVisibility(View.GONE);
        type.setText(typeStr);

        returnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        returnText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(RegisterActivity.this, "你的验证码已发送到***手机", Toast.LENGTH_LONG).show();
            }
        });

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userPhone.getText().length() < 11) {
                    Toast.makeText(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_LONG).show();
                    return;
                }
                if (codeWord.getText().length() <= 0) {
                    Toast.makeText(RegisterActivity.this, "请输入验证码", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!codeWord.getText().toString().equals("123456")) {
                    Toast.makeText(RegisterActivity.this, "验证码不正确", Toast.LENGTH_LONG).show();
                    codeWord.setText("");
                    return;
                }
                if (userPsw.getText().length() <= 0) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                if (userPsw.getText().length() < 6) {
                    Toast.makeText(RegisterActivity.this, "密码至少6位", Toast.LENGTH_LONG).show();
                    return;
                }
                if (userPswDouble.getText().length() <= 0) {
                    Toast.makeText(RegisterActivity.this, "请输入确认密码", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!userPswDouble.getText().toString().equals(userPsw.getText().toString())) {
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_LONG).show();
                    return;
                }
                insert();
            }
        });
    }

    private void insert() {
        ArrayList<LoginBean> loginBeanArrayList = new ArrayList<>();
        LoginBean loginBean = new LoginBean();
        loginBean.setUserName(userPhone.getText().toString());
        loginBean.setPswWord(userPsw.getText().toString());
        loginBeanArrayList.add(loginBean);
        // 插入新用户
        dbController.insert(loginBean);
        Toast.makeText(RegisterActivity.this, "注册成功！！！", Toast.LENGTH_LONG).show();

        this.finish();
    }


}
